﻿using OpenInterface;
using System;
using System.Collections.Generic;
using System.Text;
using HtmlAgilityPack;
using IRepository.SystemDefault;
using Service;
using Model.SystemDefault;

namespace OpenImplement
{
    /// <summary>
    /// https://www.xicidaili.com/nn/1 高匿代理 IP 页面解析
    /// </summary>
    public class Analyzer4xicidaili : IAnalyzer
    {
        IProxyIPRepository proxyIPRepository = null;

        public Analyzer4xicidaili()
        {
            proxyIPRepository = ServiceManager.GetService<IProxyIPRepository>();
        }

        public void Analyze(string url, string html, Action<string, HandlerType> actHandlerUrl)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            var root = doc.DocumentNode;

            var trs = root.SelectNodes("//*[@id=\"ip_list\"]/tr");
            for (var i = 0; i < trs.Count; i++)
            {
                if (i == 0) continue;
                var tr = trs[i];
                var tds = tr.SelectNodes("td");
                var ip = tds[1].InnerText;
                var port = tds[2].InnerText;

                proxyIPRepository.Add(new ProxyIPModel
                {
                    AddressStatus = AddressStatus.Available,
                    IPAddress = ip,
                    Port = port
                }, Common.Constant.AppSetting.DefaultDataConstr);
            }
        }
    }
}
